﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.Objective;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule.System;

/// <summary>
/// 目标类型维护
/// Update By:Allen Jiang
/// Update Time:2008-12-22
/// Description:
///     修改目标类型维护在一个页面。
/// </summary>
public partial class Objective_ObjectiveDetailMaintain : PageBase
{
    public string ObjectiveID = "";
    public string keyid = "";
    
    ObjectiveManager manager = new ObjectiveManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["OID"] != null)
            ObjectiveID = Request.QueryString["OID"].ToString();
        if (Request.QueryString["KEYID"] != null)
            keyid = Request.QueryString["KEYID"].ToString();
        if (!IsPostBack)
        {
            txtDate.Value = DateTime.Now.AddMonths(1).ToString("yyyy-MM");
            ddlBind();
            SalesBind();
            lbl_Kind.Text = getObjectKind(keyid);

            if (ObjectiveID != "")
            {
                try
                {
                    Objective o = manager.LoadObjective(int.Parse(ObjectiveID));
                    if (o != null)
                    {
                        lbl_UNIT.Text = o.Unit;
                        if (o.INPUTTYPE == 1)
                        {
                            RegularExpressionValidator1.ValidationExpression = @"^\d*";
                            RegularExpressionValidator1.ErrorMessage = "目标值应为整型";
                        }
                        else
                        {
                            RegularExpressionValidator1.ValidationExpression = @"^(\d{1,3}(,\d\d\d)*(\.\d+)?|\d+(\.\d+)?)$";
                            RegularExpressionValidator1.ErrorMessage = "目标值应为数字";
                        }

                    }
                }
                catch { }

            }
            else
                LoadData();

            ViewState["lasturl"] = Request.UrlReferrer.ToString();
            
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    ///
    public void ddlBind()
    {
        ObjectTable ot = manager.SelectObjectiveList(MyStateManager.DomainID, "");
        drpObjective.DataSource = ot;
        drpObjective.DataTextField = "NAME";
        drpObjective.DataValueField = "ID";
        drpObjective.DataBind();

        if (ObjectiveID != "")
        {
            drpObjective.SelectedValue = ObjectiveID;
            drpObjective.Enabled = false;
        }
    }
    public void SalesBind()
    {
        UserManager um = new UserManager();
        DataTable dt = um.getAllChildSalesByOrgID(MyStateManager.OrgID, MyStateManager.DomainID, true);
        CheckBoxList1.DataSource = dt;
        CheckBoxList1.DataTextField = "NAME";
        CheckBoxList1.DataValueField = "ID";
        CheckBoxList1.DataBind();
        
    }

    public string getObjectKind(string value)
    {
        string type = "";
        switch (value)
        {
            case "1":
                type = "业务员目标";
                break;
            case "2":
                type = "客户目标";
                break;
            case "3":
                type = "品牌目标";
                break;
            default:
                type = "未知";
                break;
        }
        return type;
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        try
        {
            int count = 0;
            string msg = "";
            string LogContent = "";
            Objective_Details odetils = null;
            decimal time=Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            string yearmonth = txtDate.Value.Replace("-", "").Replace("/", "");
            decimal value = decimal.Parse(txt_OBJECTIVE_DATA.Text);
            foreach (ListItem li in CheckBoxList1.Items)
            {
                if (li.Selected)
                {
                    odetils = new Objective_Details();
                    odetils.CREATETIME = time;
                   
                    //判断是否已经存在此代码
                    if (manager.ExistsObjectiveDeatils(li.Value,yearmonth, drpObjective.SelectedValue))
                    {
                        msg+= li.Text+"已存在"+txtDate.Value+"月份目标";
                        continue;
                    }

                    odetils.ObjectiveData = value;
                    odetils.YEARMONTH = yearmonth;
                    odetils.OBJECTIVEID = int.Parse(drpObjective.SelectedValue);
                    odetils.OBJECTID = li.Value;
                    odetils.Active = true;
                    odetils.DomainID = MyStateManager.DomainID;
                    odetils.LAST_UPDATER = MyStateManager.UserID.ToString();
                    odetils.LAST_UPDATE_TIME = time;
                    //keyid
                    odetils.KEYID = 1;

                    if (manager.Save(odetils))
                        count++;

                }
            }
            
            LogContent += "增加新的" + ObjectiveID + "目标明细信息："+count+" 条。";
            
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
                this.MyStateManager.UserID, LogContent);
            ShowMessage("保存成功 " + msg, ViewState["lasturl"].ToString());
           

        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                    Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                    this.MyStateManager.UserID, "添加目标明细 - 异常:" + ex.Message);
            ShowMessage("保存失败,"+ex.Message);

        }
    }
    protected void drpObjective_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadData();
    }

    public void LoadData()
    {
        if (drpObjective.SelectedValue != "")
        {
            Objective o = manager.LoadObjective(int.Parse(drpObjective.SelectedValue));
            if (o != null)
            {
                lbl_UNIT.Text = o.Unit;
                if (o.INPUTTYPE == 1)
                {
                    RegularExpressionValidator1.ValidationExpression = @"^\d*";
                    RegularExpressionValidator1.ErrorMessage = "目标值应为整型";
                }
                else
                {
                    RegularExpressionValidator1.ValidationExpression = @"^(\d{1,3}(,\d\d\d)*(\.\d+)?|\d+(\.\d+)?)$";
                    RegularExpressionValidator1.ErrorMessage = "目标值应为数字";
                }

            }
        }
    }
}
